package com.example.demo.controller;

import com.example.demo.model.*;
import com.example.demo.service.MyService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
public class MainController {
    @Autowired
    MyService ms;

    @RequestMapping("/cfp")
    public List<Clothes> showCFP(){
        List<Clothes> list = ms.selectAllClothes();
        return list;
    }
    @RequestMapping("/zhp")
    public List<Product> showZHP(){
        List<Product> list = ms.selectAllProduct();
        return list;
    }
    @RequestMapping("/beibao")
    public List<Bag> showBag(){
        List<Bag> list = ms.selectAllBag();
        return list;
    }
    @RequestMapping("/zhuangbei")
    public Player zhuangbei(@RequestParam("bag")String s){
        Bag bag = new Gson().fromJson(s,Bag.class);
        System.out.println(bag);
        ms.equip(bag);
        Player player = ms.selectPlayer(1);
        return player;
    }
    @RequestMapping("/jiajv")
    public List<Furniture> showFurniture(){
        List<Furniture> list = ms.selectAllFurniture();
        return list;
    }
    @RequestMapping("/yichu")
    public List<Bag> showClothes(){
        List<Bag> list = ms.selectByType(2);
        return list;
    }
    @RequestMapping("/riji")
    public List<Diary> showDiary(){
        List<Diary> list = ms.readDiary();
        return list;
    }
    @RequestMapping("/writeDiary")
    public int writeDiary(@RequestParam("text")String text){
        int x = ms.writeDiary(text);
        return x;
    }
    @RequestMapping("/buyClothes")
    public Player buyClothes(@RequestParam("clothes") String s){
        Clothes clothes = new Gson().fromJson(s,Clothes.class);
        System.out.println(clothes);
        int x = ms.buyClothes(clothes);
        Player player = ms.selectPlayer(1);
        return player;
    }
    @RequestMapping("/buyProduct")
    public Player buyProduct(@RequestParam("product") String s){
        Product product = new Gson().fromJson(s,Product.class);
        int x = ms.buyProduct(product);
        Player player = ms.selectPlayer(1);
        return player;
    }

    @RequestMapping("/tansuo")
    public String tansuo(){
        Random r = new Random();
        int rNum = r.nextInt(100);
        if(rNum>=0&&rNum<20){
            return "获得了树枝。要做什么";
        }else if(rNum>=20&&rNum<40){
            return "获得了石头。要做什么";
        }else if(rNum>=40&&rNum<50){
            return "获得了破布。要做什么";
        }else{
            return "敌人出现了！";
        }

    }

    @RequestMapping("/tansuo2")
    public String tansuo2(){
        Random r = new Random();
        int rNum = r.nextInt(100);
        if(rNum>=0&&rNum<20){
            return "获得了粘液。要做什么";
        }else if(rNum>=20&&rNum<40){
            return "获得了毒牙。要做什么";
        }else if(rNum>=40&&rNum<60){
            return "获得了暗影之魂。要做什么";
        }else{
            return "敌人出现了！";
        }

    }

    @RequestMapping("/player")
    public Player playerInit(){
        Player player = ms.selectPlayer(1);
        return player;
    }

    @RequestMapping("/enemy")
    public Enemy Enemy(){
        Enemy enemy = ms.randomSelect("树林");
        return enemy;
    }
    @RequestMapping("/enemy2")
    public Enemy Enemy2(){
        Enemy enemy = ms.randomSelect("沼泽");
        return enemy;
    }

    @RequestMapping("/save")
    public int save(@RequestParam("list") String s,@RequestParam("money") int m){

        List<String> list = new Gson().fromJson(s,new TypeToken<List<String>>() {}.getType());
        System.out.println(list);
        ms.save(list);
        ms.updateMoney(m);
        return 0;
    }

    @RequestMapping("/queryThings")
    public List<Thing> showThings(){
        List<Thing> list = ms.selectAllThing();
        return list;
    }

    @RequestMapping("/useThing")
    public int useThing(@RequestParam("thing")String s){
        Thing thing = new Gson().fromJson(s,Thing.class);
        ms.useThing(thing);
        return 0;
    }


}
